package org.csu.sei.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiOperation;
import org.csu.sei.common.BaseConstant;
import org.csu.sei.common.ImageUpload;
import org.csu.sei.common.ReturnJson;
import org.csu.sei.entity.TeacherInfo;
import org.csu.sei.service.ITeacherInfoService;
import org.csu.sei.service.IUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

import static org.csu.sei.common.BaseConstant.ErrorCode.*;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private ITeacherInfoService teacherInfoService;

    @Resource
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询教师信息页面")
    public JSONObject queryTeacher(@PathVariable Long id){
        TeacherInfo teacherInfo = teacherInfoService.queryTeacherInfo(id);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("cid", BaseConstant.TeacherCat.TEACHER_CATEGORY_ID);
        if(teacherInfo==null){
            teacherInfo = new TeacherInfo();
            teacherInfo.setName(userService.getById(id).getUsername());
        }
        jsonObject.put("teacherInfo",teacherInfo);
        return ReturnJson.successRes(jsonObject);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "新建或更新教师信息页面",notes = "教师初次登陆之后，在数据库中新增教师信息；若已存在，则进行更新")
    public JSONObject insertOrUpdateTeacherInfo(@RequestBody TeacherInfo teacherInfo, @PathVariable Long id){
        teacherInfo.setId(id);
        int result = teacherInfoService.insertOrUpdateTeacherInfo(teacherInfo);
        if(result==-1){
            return ReturnJson.failureResWithMsg(USER_NOT_EXIST,"teacher not exist");
        }
        else if(result==-2){
            return ReturnJson.defaultFailureRes(TEACHER_NOT_TEACHER);
        }
        return ReturnJson.ackWithMsg("ok");
    }

//    @PostMapping("")
//    @ApiOperation(value = "新建教师信息页面",notes = "教师初次登陆之后，在数据库中新增教师信息")
//    public JSONObject insertTeacherInfo(
//            @RequestParam(value = "id") Long id,
//            @RequestParam(value = "img",required = false) String img,
//            @RequestParam(value = "name") String name,
//            @RequestParam(value = "employeeNumber",required = false) String employeeNumber,
//            @RequestParam(value = "jobTitle") String jobTitle,
//            @RequestParam(value = "sex",required = false) int sex,
//            @RequestParam(value = "contactInformation",required = false) String contactInformation,
//            @RequestParam(value = "position",required = false) String position,
//            @RequestParam(value = "degree",required = false) String degree,
//            @RequestParam(value = "organization",required = false) String organization,
//            @RequestParam(value = "location",required = false) String location,
//            @RequestParam(value = "direction",required = false) String direction,
//            @RequestParam(value = "introduction",required = false) String introduction,
//            @RequestParam(value = "scientificResearch",required = false) String scientificResearch,
//            @RequestParam(value = "paper",required = false) String paper,
//            @RequestParam(value = "honour",required = false) String honour,
//            @RequestParam(value = "teaching",required = false) String teaching
//            ){
//        TeacherInfo teacherInfo = new TeacherInfo();
//        teacherInfo.setId(id);
//        teacherInfo.setImg(img);
//        teacherInfo.setName(name);
//        teacherInfo.setEmployeeNumber(employeeNumber);
//        teacherInfo.setJobTitle(jobTitle);
//        teacherInfo.setSex(sex);
//        teacherInfo.setContactInformation(contactInformation);
//        teacherInfo.setPosition(position);
//        teacherInfo.setDegree(degree);
//        teacherInfo.setOrganization(organization);
//        teacherInfo.setLocation(location);
//        teacherInfo.setDirection(direction);
//        teacherInfo.setIntroduction(introduction);
//        teacherInfo.setScientificResearch(scientificResearch);
//        teacherInfo.setPaper(paper);
//        teacherInfo.setHonour(honour);
//        teacherInfo.setTeaching(teaching);
//
//        int result = teacherInfoService.insertTeacherInfo(teacherInfo);
//        if(result==-1){
//            return ReturnJson.failureResWithMsg(USER_NOT_EXIST,"userid not exist");
//        }
//        else if(result==-2){
//            return ReturnJson.defaultFailureRes(TEACHER_NOT_TEACHER);
//        }
//        else if(result==-3){
//            return ReturnJson.defaultFailureRes(TEACHER_INFO_EXIST);
//        }
//        return ReturnJson.ackWithMsg("ok");
//    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除教师信息页面",notes = "删除数据库中的教师信息，只删除teacher_info里的，user里的删除")
    public JSONObject deleteTeacherInfo(@PathVariable Long id){
        int result=teacherInfoService.deleteTeacherInfo(id);
        if(result==-1){
            return ReturnJson.failureResWithMsg(USER_NOT_EXIST,"teacher not exist");
        }
        return ReturnJson.ackWithMsg("ok");
    }

//    @PutMapping("/{id}")
//    @ApiOperation(value = "更新教师信息",notes = "教师初次登陆之后，在数据库中新增教师信息")
//    public JSONObject updateTeacherInfo(
//            @PathVariable Long id,
//            @RequestParam("img") String img,
//            @RequestParam("name") String name,
//            @RequestParam("employeeNumber") String employeeNumber,
//            @RequestParam("jobTitle") String jobTitle,
//            @RequestParam("sex") int sex,
//            @RequestParam("contactInformation") String contactInformation,
//            @RequestParam("position") String position,
//            @RequestParam("degree") String degree,
//            @RequestParam("organization") String organization,
//            @RequestParam("location") String location,
//            @RequestParam("direction") String direction,
//            @RequestParam("introduction") String introduction,
//            @RequestParam("scientificResearch") String scientificResearch,
//            @RequestParam("paper") String paper,
//            @RequestParam("honour") String honour,
//            @RequestParam("teaching") String teaching
//    ){
//        TeacherInfo teacherInfo = new TeacherInfo();
//        teacherInfo.setId(id);
//        teacherInfo.setImg(img);
//        teacherInfo.setName(name);
//        teacherInfo.setEmployeeNumber(employeeNumber);
//        teacherInfo.setJobTitle(jobTitle);
//        teacherInfo.setSex(sex);
//        teacherInfo.setContactInformation(contactInformation);
//        teacherInfo.setPosition(position);
//        teacherInfo.setDegree(degree);
//        teacherInfo.setOrganization(organization);
//        teacherInfo.setLocation(location);
//        teacherInfo.setDirection(direction);
//        teacherInfo.setIntroduction(introduction);
//        teacherInfo.setScientificResearch(scientificResearch);
//        teacherInfo.setPaper(paper);
//        teacherInfo.setHonour(honour);
//        teacherInfo.setTeaching(teaching);
//
//        int result=teacherInfoService.updateTeacherInfo(teacherInfo);
//        if(result==-1){
//            return ReturnJson.failureResWithMsg(USER_NOT_EXIST,"teacher not exist");
//        }
//        return ReturnJson.ackWithMsg("ok");
//    }

    @GetMapping("/list")
    @ApiOperation(value = "教师信息列表",notes = "教师信息列表，显示：id、学工号、姓名、图片、创建时间、更新时间")
    public JSONObject TeacherInfoList(){
        List teacherInfoItemDTOList = teacherInfoService.TeacherInfoList();
        JSONObject json = new JSONObject();
        json.put("teacher_list",teacherInfoItemDTOList);
        return ReturnJson.successRes(json);
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片至OSS服务器并返回图片的链接地址")
    public JSONObject uploadImage(@RequestParam("image_upload") MultipartFile imageUpload){
        JSONObject json = new JSONObject();
        String url="";
        try{
            url= ImageUpload.getUrl(BaseConstant.OSSConfig.TEACHER_IMG_DIR,imageUpload);
            json.put("image_url",url);
            return ReturnJson.successRes(json);
        }
        catch (Exception e){
            return ReturnJson.defaultFailureRes(IMAGE_UPLOAD_OSS_ERROR);
        }
    }

}
